
// config
import { LOCAL_ACCOUNT, LOCAL_AUTH } from "project/config";
// state
import { makeAutoObservable, toJS } from "mobx";
// script & methods & public
import storageUtils from "utils/storageUtils";
// 其它

class UserState {
  account: Account = {
    // avatar: "",
    email: "",
    created: "",
    userId: 0,
    phone: "",
    userName: "",
    fullName: "",
    menuList: [], 
    permissions: [],
  };
  token = "";
  constructor() {
    makeAutoObservable(this);
  }
  setAccount(data: Partial<Account>) {
    const old = this.getAccount();
    this.account = {
      ...old,
      ...data,
    };
    storageUtils.setStore(LOCAL_ACCOUNT, this.getAccount(), true);
  }
  cleanData(){
    storageUtils.clearAllStore();
  }
  getAccount() {
    return toJS(this.account);
  }
  getPermissions() {
    return toJS(this.account.permissions.map((el) => el.code));
  }
  setToken(val = "") {
    this.token = val;
    storageUtils.setStore(LOCAL_AUTH, val, true);
  }
  getToken() {
    return toJS(this.token);
  }
  isLogin() {
    return this.token !== "";
  }
  isUser(){
    return !!this.account.idCard
  }
}

export default UserState;
